package com.java.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.java.model.hosp.Department;
import com.java.model.vo.DepartmentQueryVo;
import com.java.model.vo.DepartmentVo;
import com.java.repository.DepartmentRespository;
import com.java.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRespository departmentRespository;

    @Override
    public void save(Map<String, Object> paramMap) {
        //map集合转对象
        String jsonString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);

        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();

        Department department1 = departmentRespository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        if(department1 != null){
            //存在修改
            department1.setCreateTime(department.getCreateTime());
            department1.setUpdateTime(new Date());
            department1.setIsDeleted(0);
            departmentRespository.save(department1);
        }else{
            //不存在添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRespository.save(department);
        }
    }

    @Override
    public Page<Department> findByPage(int pageNum, int pageSize, DepartmentQueryVo departmentQueryVo) {

        //分页数据
        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        //创建实例
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        Example<Department> example = Example.of(department, matcher);

        //条件分页查询
        Page all = departmentRespository.findAll(example,pageable);
        return all;
    }

    @Override
    public void removeById(String hoscode, String depcode) {
        //根据hoscode和depcode查询出 department
        Department department = departmentRespository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            departmentRespository.deleteById(department.getId());
        }
    }

    //通过hoscode 获取科室树
    @Override
    public List<DepartmentVo> findByHoscode(String hoscode) {
        ArrayList<DepartmentVo> result = new ArrayList<>();
        //获取数据
        Department department = new Department();
        //根据hoscode查询出所有的科室信息
        department.setHoscode(hoscode);
        //mongodb条件构造器
        Example<Department> example = Example.of(department);
        //[{department},{department}] 所有的科室列表信息
        List<Department> departmentList = departmentRespository.findAll(example);
        //java8 stream()遍历  以getBigcode进行分组
        Map<String, List<Department>> stringListMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //将map进行遍历
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()){
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(entry.getKey());
            departmentVo1.setDepname(entry.getValue().get(0).getDepname());
            ArrayList<DepartmentVo> children = new ArrayList<>();
            for (Department dept : entry.getValue()){
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(dept.getDepcode());
                departmentVo2.setDepname(dept.getDepname());
                children.add(departmentVo2);
            }
            departmentVo1.setChildren(children);
            result.add(departmentVo1);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRespository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRespository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
